<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\Activity\FlashSale;
use App\Models\Activity\GroupProduct;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class ActivityController extends Controller
{
    public function groupProducts(Request $request){
        //判断商品是否是活动商品，活动是否已过期
        if ($product['activity_type'] == 2){
            $group_product = GroupProduct::where('product_id',$product['id'])->orderBy('created_at','desc')->first();
            $now = Carbon::now();
            if ($now->gt($group_product['deadline'])){
                $product['price'] = $group_product['show_price'];
                $product['name'] = $group_product['group_name'];
                $product['activity_id'] = $group_product['id'];
                $order_type = 2;
                $quantity += $products[$i]['count'];
            }
        }
    }

    public function flashSales(Request $request){
        //秒杀商品检测
        if ($product['activity_type'] == 3){
            $flash_sale = FlashSale::where('product_id',$product['id'])->orderBy('created_at','desc')->first();
            $now = Carbon::now();
            if ($now->gt($flash_sale['deadline'])){
                //TODO：此处要不要加数据锁
                if ($flash_sale['stock']>0){
                    $product['price'] = $flash_sale['show_price'];
                    $product['name'] = $flash_sale['product_name'];
                    $product['activity_id'] = $flash_sale['id'];
                    $order_type = 3;
                    $quantity += $products[$i]['count'];
                }else{
                    return [
                        'result'=>"failed",
                        'message'=>"商品已被抢购完",
                    ];
                }
            }else{
                return [
                    'result'=>"failed",
                    'message'=>"秒杀活动已结束",
                ];
            }
        }
    }


}
